/**
	Pulsar engine. Core.
	Additional string functions

	Copyright (C) 2013-2014 Dmitry Dolgov

	This program is free software; you can redistribute it and/or modify it under the terms of the
	GNU General Public License as published by the Free Software Foundation; either version 2 of
	the License, or (at your option) any later version.
		
	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
	without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	See the GNU General Public License for more details.
*/

namespace pl
{

/// Convert values to string (internal functions)
extern pl_char* _num2str(pl_char* dest, short value, int radix);
extern pl_char* _num2str(pl_char* dest, char value, int radix);
extern pl_char* _num2str(pl_char* dest, unsigned char value, int radix);
extern pl_char* _num2str(pl_char* dest, unsigned short value, int radix);
extern pl_char* _num2str(pl_char* dest, int value, int radix);
extern pl_char* _num2str(pl_char* dest, unsigned int value, int radix);
extern pl_char* _num2str(pl_char* dest, long value, int radix);
extern pl_char* _num2str(pl_char* dest, unsigned long value, int radix);
extern pl_char* _num2str(pl_char* dest, long long value, int radix);
extern pl_char* _num2str(pl_char* dest, unsigned long long value, int radix);
extern pl_char* _num2str(pl_char* dest, float value);
extern pl_char* _num2str(pl_char* dest, double value);
extern pl_char* _num2str(pl_char* dest, long double value);

// Convert string to value (internal functions)
extern void _str2num(const pl_char* first, unsigned char& value, const pl_char** endChar = NULL, int radix = -1);
extern void _str2num(const pl_char* first, char& value, const pl_char** endChar = NULL, int radix = -1);
extern void _str2num(const pl_char* first, short& value, const pl_char** endChar = NULL, int radix = -1);
extern void _str2num(const pl_char* first, unsigned short& value, const pl_char** endChar = NULL, int radix = -1);
extern void _str2num(const pl_char* first, int& value, const pl_char** endChar = NULL, int radix = -1);
extern void _str2num(const pl_char* first, unsigned int& value, const pl_char** endChar = NULL, int radix = -1);
extern void _str2num(const pl_char* first, long& value, const pl_char** endChar = NULL, int radix = -1);
extern void _str2num(const pl_char* first, unsigned long& value, const pl_char** endChar = NULL, int radix = -1);
extern void _str2num(const pl_char* first, long long& value, const pl_char** endChar = NULL, int radix = -1);
extern void _str2num(const pl_char* first, unsigned long long& value, const pl_char** endChar = NULL, int radix = -1);
extern void _str2num(const pl_char* first, float& value, const pl_char** endChar = NULL, int radix = -1);
extern void _str2num(const pl_char* first, double& value, const pl_char** endChar = NULL, int radix = -1);
extern void _str2num(const pl_char* first, long double& value, const pl_char** endChar = NULL, int radix = -1);

/// Convert integer value to string
template<pl_uint32 N, typename T> String<N> int2str(T value, int radix)
{
	pl_char buffer[N];
	return String<N>(buffer, _num2str(buffer, value, radix));
}

/// Convert floating point value to string
template<pl_uint32 N, typename T> String<N> float2str(T value)
{
	pl_char buffer[N];
	return String<N>(buffer, _num2str(buffer, value));
}

/// Convert string to integer
template<typename T> T str2num(const pl_char *str, const pl_char** endChar = NULL, int radix = -1)
{
	T value;
	_str2num(str, value, endChar, radix);
	return value;
}

/// Convert string to integer
template<typename T, pl_uint32 N> T str2num(const String<N> &str, int radix = -1)
{
	T value;
	_str2num(str.get(), value, NULL, radix);
	return value;
}

/// Calculate string CRC
extern pl_uint32 stringChecksum(const pl_char* str);

/// Calculate string CRC with limited length
extern pl_uint32 stringChecksum(const pl_char *buffer, pl_uint32 size);

template<pl_uint32 N> pl_uint32 stringChecksum(const String<N> &str)
{
	return stringChecksum(str.get(), str.size());
}

/// Calculate any buffer CRC
extern pl_uint32 bufferChecksum(const void *buffer, pl_uint32 size);

/// Reverse characters between first and last
extern void reverseString(pl_char *first, pl_char *last);

} // namespace pl

//
// End of file 'pulsar_string_util.h'
//
